﻿using System.Collections.Generic;
using System.Web;
using NExt.Handler.Base.Default;

namespace NExt.Handler.Base
{
    public abstract class JsHandlerFactory : IHandlerFactory
    {
        private readonly IHttpHandler handler;

        protected JsHandlerFactory()
        {
            IDictionary<string, string> headerParameters = new Dictionary<string, string>();
            headerParameters.Add("Content-Type", "application/x-javascript");
            IContentBuilder contentBuilder = BuildContentBuilder();
            IReponseStreamBuilder reponseStreamBuilder = new DefaultResponseStreamBuilder(contentBuilder);
            IResponseBuilder responseBuilder = new DefaultResponseBuilder(headerParameters, reponseStreamBuilder);
            handler = new DefaultHandler(false, responseBuilder);
        }

        protected abstract IContentBuilder BuildContentBuilder();

        public abstract bool CanHandle(HttpContext context, string requestType, string url, string pathTranslated);

        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            return handler;
        }

        public void ReleaseHandler(IHttpHandler httpHandler)
        {
        }
    }
}
